package com.nti56.springboot.controller;



import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.nti56.springboot.annotation.SystemControllerLog;
import com.nti56.springboot.model.AjaxResult;
import com.nti56.springboot.model.Dict;
import com.nti56.springboot.model.DictItem;
import com.nti56.springboot.model.PageResponse;
import com.nti56.springboot.service.DictItemService;
import com.nti56.springboot.service.DictService;
import com.nti56.springboot.utils.StringUtil;



@RequestMapping("dict")
@Controller
public class DictController extends BaseController{
    
	private static final Logger logger = LoggerFactory.getLogger(DictController.class);

	
	@Autowired
    private DictService dictService;
	
	@Autowired
    private DictItemService dictItemService;
	
	@RequestMapping("/toDictList")
    public String toDictList(Map<String,Object> map){
        return "system/dictList";
    }
	
	

	@RequestMapping("toDictAdd")
	public String toDictAdd(HttpServletRequest request, Model model){
		String dictId = (String) request.getParameter("dictId");
		if(!StringUtil.isEmpty(dictId)) {
			Dict dict = dictService.getById(dictId);
			model.addAttribute("dict", dict);
		}
		model.addAttribute("dictId", dictId);
        return "system/dictAdd";
	}
	
	@RequestMapping("/addDict")
    @ResponseBody
    @SystemControllerLog(description = "保持数据字典",actionType = "1")
    public AjaxResult addDict(HttpServletRequest request,Dict dict){    	
    	try {
    		Map map = this.getParamMap(request);
    		String entities = (String)map.get("entities");
    		List<DictItem> results = JSON.parseArray("["+entities+"]", DictItem.class);
    		String dictId = (String) request.getParameter("dictId");
			if(StringUtil.isEmpty(dictId)) {
				dictService.insert(dict,results);
				return new AjaxResult(true,AjaxResult.SAVE_OK);
			}else {
				dictService.update(dict,results);
				return new AjaxResult(true,AjaxResult.UPDATE_OK);
			}			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return new AjaxResult(false,e.getMessage(),AjaxResult.SAVE_FAIL);
		}
    }
	
	
	@RequestMapping("/listDicts")
    @ResponseBody
    public PageResponse listDicts(HttpServletRequest request){
    	Map map = this.getParamMap(request);
    	this.startPage(request);
        List<Dict> list = dictService.findAll(map);
        return this.getPageResponse(list);
    }
	
	
	@RequestMapping("/listDictItems")
    @ResponseBody
    public PageResponse listDictItems(HttpServletRequest request){
    	Map map = this.getParamMap(request);
    	this.startPage(request);
        List<DictItem> list = dictItemService.findAll(map);
        return this.getPageResponse(list);
    }
	
	@RequestMapping("/deleteDict")
    @ResponseBody
    public AjaxResult deleteDict(HttpServletRequest request){
    	try {
    		Map map = this.getParamMap(request);
        	String ids = (String) map.get("ids");
    		List<String> list = Arrays.asList(ids.split(","));
    		for(String dictId : list){
    			dictService.delete(dictId);
    			dictItemService.deleteByDictId(dictId);
    		}
			return new AjaxResult(true,AjaxResult.DLELETE_OK);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return new AjaxResult(false,e.getMessage(),AjaxResult.DLELETE_FAIL);
		}
    }
}